//
//  VisitorLoginView.swift
//  01sinablog
//
//  Created by ZhangZiang on 15/12/12.
//  Copyright © 2015年 ZhangZiang. All rights reserved.
//

import UIKit

//设置代理让控制器去执行按钮点击后的逻辑
protocol VisitorLoginViewDelegate : NSObjectProtocol
{
    func userWillLogin()
    
    func userWillRegister()
}

class VisitorLoginView: UIView {

    weak var visitorViewDelegate : VisitorLoginViewDelegate?
    
    func loginBtnDidClicked()
    {
        visitorViewDelegate?.userWillLogin()
    }
    
    func registerBtnDidClicked()
    {
        visitorViewDelegate?.userWillRegister()
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //加一个公共的方法,去判断是从哪个界面进来的
    func setupVisitorInfo(tiptext : String, imageName : String?)
    {
        tipLabel.text = tiptext
        tipLabel.sizeToFit()
        
        //Home界面传nil,其他的界面进来都都传来一个imageName
        if let name = imageName
        {
            //不是Home界面,隐藏House
            centerHouse.hidden = true
            //把中间图片移动到backView的上面
            bringSubviewToFront(centerImage)

            centerImage.image = UIImage(named: name)
        }
        else
        {
            //是Home界面,开启动画
            animateHouse()
        }
    }
    //开启动画的方法
    func animateHouse()
    {
        let animate = CABasicAnimation(keyPath: "transform.rotation")
        
        animate.repeatCount = MAXFLOAT
        animate.duration = 20
        animate.toValue = 2 * M_PI
        //切换控制器或者动画完成时候不移除动画
        animate.removedOnCompletion = false
        
        centerImage.layer.addAnimation(animate, forKey: nil)
    }
    
    func setupUI()
    {
        addSubview(centerImage)
        addSubview(backView)
        addSubview(centerHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)

        
        //中间的图形
        addConstraint(NSLayoutConstraint(item: centerImage, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: centerImage, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -60))
        
        //中间的房子
        addConstraint(NSLayoutConstraint(item: centerHouse, attribute: .CenterX, relatedBy: .Equal, toItem: centerImage, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: centerHouse, attribute: .CenterY, relatedBy: .Equal, toItem: centerImage, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //tip label
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: centerImage, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: centerImage, attribute: .Bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
        tipLabel.numberOfLines = 0
        
        //按钮的约束
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        //If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        

        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView" : backView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-35)-[regBtn]", options: [], metrics: nil, views: ["backView" : backView, "regBtn" : registerBtn]))
        
        //取消所有子视图的autoresizing
        cancelAutoResizing()
    }
    
    func cancelAutoResizing()
    {
        for subview in subviews
        {
            subview.translatesAutoresizingMaskIntoConstraints = false
        }
    }
    
    //懒加载控件
    private lazy var centerImage : UIImageView = {
        let centerImage = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        centerImage.sizeToFit()
        return centerImage
    }()
    
    private lazy var centerHouse : UIImageView = {
        let centerHouse = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return centerHouse
    }()
    
    private lazy var loginBtn : UIButton = {
        let loginBtn = UIButton()
        
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        loginBtn.setTitle("登录", forState: .Normal)
        loginBtn.setTitleColor(UIColor.blackColor(), forState: .Normal)
        loginBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        loginBtn.addTarget(self, action: "loginBtnDidClicked", forControlEvents: .TouchUpInside)
        
        return loginBtn
    }()
    
    private lazy var registerBtn : UIButton = {
        let registerBtn = UIButton()
        
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        registerBtn.setTitle("注册", forState: .Normal)
        registerBtn.setTitleColor(UIColor.blackColor(), forState: .Normal)
        registerBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        registerBtn.addTarget(self, action: "registerBtnDidClicked", forControlEvents: .TouchUpInside)
        
        return registerBtn
    }()
    
    private lazy var tipLabel : UILabel = {
        let tipLabel = UILabel()
        tipLabel.textColor = UIColor.blackColor()
        tipLabel.font = UIFont.systemFontOfSize(15)
        return tipLabel
    }()
    
    private lazy var backView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
}
